<?php
	#-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Xml.KumomadoXmlWriter');
	/**
	 * @package libraries/xml/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoXsltProcessor {
		public $xml;
		private $xslt;
		public function __construct() {
			$xml_writer = new KumomadoXmlWriter('Result');
			$this->xml = $xml_writer->getDocument();
		}
		/*public function importXml($xml) {
			if (get_class($xml) == 'DOMDocument') {
				$this->xml = $xml;
			}
		}*/
		/*public function importAssocArray($array) {
			$xml_writer = new KumomadoXmlWriter('Result');
			$xml_writer->fromAssocArray($array);
			$this->xml = $xml_writer->getDocument();
		}*/
		public function importArray($array) {
			$xml_writer = new KumomadoXmlWriter('Result');
			$xml_writer->fromArray($array);
			$this->xml = $xml_writer->getDocument();
		}
		public function importStyleSheet($xslt, $path = '') {
			if ($path != '') {
				$xslt = $path.$xslt;
			}
			if (file_exists($xslt)) {
				$this->xslt = $xslt;
			} else {
				throw new KumomadoXsltProcessorException('The XSLT file('.$xslt.') is not exists.');
			}
		}
		public function transformToDoc() {
			$xml_doc = new DOMDocument;
			$xml_doc->loadXML($this->xml);
			$xslt_doc = new DOMDocument;
			$xslt_doc->load($this->xslt);
			$processor = new XSLTProcessor();
			$processor->importStyleSheet($xslt_doc);
			$dom = $processor->transformToDoc($xml_doc);
			if (get_class($dom) == 'DOMDocument') {
				$html = $dom->saveHTML();
				$html = preg_replace('/[(\t)(\n)]/i', '', $html);
				return htmlspecialchars_decode($html);
			} else {
				return '';
			}
		}
	}
?>